package 第一题;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {

    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;

        try {
            serverSocket = new ServerSocket(8888);
            System.out.println("服务器已经启动。。。");

            int count=0;
            while (true) {
                socket = serverSocket.accept();

                new Thread(new ServerThread(socket)).start();
                count++;
                System.out.println(socket.getInetAddress() + "\t客户端已经连接上了,当前客户端连接数："+count+"个");


                /*Socket finalSocket = socket;
                new Thread(()->{
                    BufferedReader br = null;
                    ObjectOutputStream oos = null;
                    InputStream is = null;
                    try {
                        br = new BufferedReader(new InputStreamReader(finalSocket.getInputStream()));
                        oos = new ObjectOutputStream(finalSocket.getOutputStream());
                        is = finalSocket.getInputStream();
                        while (true) {
                            String num = br.readLine();
                            if (num.equals("0")) {
                                System.out.println(finalSocket.getInetAddress() + "\t这个用户已经退出");
                                break;
                            } else if (num.equals("1")) {
                                System.out.println("他要查询全部学生信息");
                                ArrayList<Student> list = (ArrayList<Student>) StudentBusiness.showAllStudent();
                                oos.writeObject(list);
                            } else if (num.equals("2")) {
                                System.out.println("他要根据查询部分学生信息");
                                String name = br.readLine();
                                List<Student> studentList = StudentBusiness.findByName(name);
                                oos.writeObject(studentList);
                                oos.flush();
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }).start();*/
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
